var allAlbum=[];
$(function(){
    //侧边栏
    $("#nav-main2").addClass('nav-active');
    $("#nav-main2").find(".nav-icon-img").attr("src",'/Extend/images/aside-icon/addBuilding1.svg');
    $("#nav-main2").find('.nav-triangle').fadeIn(200);
    $(".nav-child").slideUp(300)

    //宣传图
    var _editor;//重新实例化一个编辑器，防止在上面的editor编辑器中显示上传的图片或者文件
    _editor = UE.getEditor('upload_ue');
    _editor.ready(function () {
        _editor.setDisabled();//编辑器不可用
        _editor.hide();//隐藏编辑器
        //侦听图片上传
        _editor.addListener('beforeInsertImage', function (t, arg) {
            if (arg!=''){
                //存传给后台的值
                $("#design_img").attr("value", arg[0].src);
                //图片预览
                $("#showDesignImg").attr("src", arg[0].src);
            }
        })
    });
    //弹出图片上传对话框
    $('.upload').on('click',function(){
        var myImage = _editor.getDialog("insertimage");
        myImage.open();
        return false;
    })

    //背景图
    var _editor1;//重新实例化一个编辑器，防止在上面的editor编辑器中显示上传的图片或者文件
    _editor1 = UE.getEditor('upload_ue1');
    _editor1.ready(function () {
        _editor1.setDisabled();//编辑器不可用
        _editor1.hide();//隐藏编辑器
        //侦听图片上传
        _editor1.addListener('beforeInsertImage', function (t, arg) {
            if (arg!=''){
                //存传给后台的值
                $("#back_img").attr("value", arg[0].src);
               //图片预览
               $("#showBackImg").attr("src", arg[0].src);
               console.log(arg[0].src)
            }
        })
    });
    //弹出图片上传对话框
    $('.upload1').on('click',function(){
        var myImage = _editor1.getDialog("insertimage");
        myImage.open();
        return false;
    })



    //单图上传
    var _editor2;//重新实例化一个编辑器，防止在上面的editor编辑器中显示上传的图片或者文件
    _editor2 = UE.getEditor('upload_ue2');
    _editor2.ready(function () {
        _editor2.setDisabled();//编辑器不可用
        _editor2.hide();//隐藏编辑器
        //侦听图片上传
        _editor2.addListener('beforeInsertImage', function (t, arg) {
            if (arg!=''){
                //存传给后台的值
                $("#single_img").attr("value", arg[0].src);
                //图片预览
                $("#showSingleImg").attr("src", arg[0].src);
                console.log(arg[0].src)
            }
        })
    });
    //弹出图片上传的对话框
    $('.upload2').on('click',function(){
        var myImage = _editor2.getDialog("insertimage");
        myImage.open();
        return false;
    })

    //多图上传
    var _editor3;
    _editor3 = UE.getEditor('upload_ue3');
    _editor3.ready(function () {
        _editor3.setDisabled();
        _editor3.hide();
        _editor3.addListener('beforeInsertImage', function (t, arg) {
            var argNum = arg.length;
            var img = [];
            if (argNum!=0){
                for (var i=0;i<argNum;i++){
                    console.log(arg[i].src)
                    $(".alert-add-album-warp").append(
                        '<div class="img-list">' +
                        '<img class="alert-album-img" src="' + arg[i].src+ '" >' +
                        '<img class="delete-alert-album-img" src="/Extend/images/delete.svg" onclick="delInput(this)">' +
                        '</div>'
                    )
                    allAlbum.push(arg[i].src);
                }
            }
        })
    });
    //弹出图片上传的对话框
    $('.upload3').on('click',function(){
        var myImage = _editor3.getDialog("insertimage");
        myImage.open();
        return false;
    })


    //图文上传
    var _editor4;//重新实例化一个编辑器，防止在上面的editor编辑器中显示上传的图片或者文件
    _editor4 = UE.getEditor('upload_ue4');
    _editor4.ready(function () {
        _editor4.setDisabled();//编辑器不可用
        _editor4.hide();//隐藏编辑器
        //侦听图片上传
        _editor4.addListener('beforeInsertImage', function (t, arg) {
            if (arg!=''){
                //存传给后台的值
                $("#article_img").attr("value", arg[0].src);
                //图片预览
                $("#articleImg").attr("src", arg[0].src);
                console.log(arg[0].src)
            }
        })
    });
    //弹出图片上传的对话框
    $('.upload4').on('click',function(){
        var myImage = _editor4.getDialog("insertimage");
        myImage.open();
        return false;
    })


   //视频上传
    $(".video-sucess").hide()
    $(".alert-video-add").attr("disabled","disabled");
    $(".alert-video-add").css({
        "cursor":"notAllowed",
        "background":"#ccc"
    })
    $('.up-video-btn').click(function(){
        var formData = new FormData($('#postForm')[0]);
        $.ajax({
            url : "/manage/upload/video",
            type : "POST",
            data : formData,
            contentType:false,
            processData:false,
            success : function(data) {
                var newData=data[0].substring(1)
                console.log(newData)
                $(".alert-video-path").val(newData)
                $(".video-sucess").show()
                $(".alert-video-add").attr("disabled",false);
                $(".alert-video-add").css({
                    "cursor":"pointer",
                    "background":"#b52134"
                })
            }
        });
    })
    //保存
    $('.save').click(function(){
        var input_val = {};
        $(".uopload-infomation").each(function () {
            var name = $(this).attr("name");
            input_val[name] = $(this).val();
        })
        console.log(input_val)
        $.ajax({
            type:'post',
            dataType:'json',
            url:'/manage/building/inserts',
            data:input_val,
            success:function (msg) {
                console.log(msg)
                if (msg.code == '1') {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        window.location.href='/manage/index/inserts?id='+msg.id;
                    })
                }else if(msg.code == '2'){
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        window.location.href='/manage/index/index?mark=qb&company=fj';
                    })
                } else {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        $('.alert-info').fadeOut(300);
                    })
                }
            }
        })
    })

    //选择类别
    $(".content-add").click(function(){
        $(".alert-type").fadeIn(200)
    })
    //类别悬停样式
    $(".alert-type-ul>li").mouseenter(function(){
        var which=$(this).find(".alert-type-img").attr("data-name");
        var path="/Manage/images/"+which+"1.svg"
        $(this).find(".alert-type-img").attr("src",path)
    }).mouseleave(function(){
        var which=$(this).find(".alert-type-img").attr("data-name");
        var path="/Manage/images/"+which+".svg"
        $(this).find(".alert-type-img").attr("src",path)
    })
    $(".alert-type-ul>li").click(function(){
        var which=$(this).find(".alert-type-img").attr("data-name");
        $(".alert-"+which+"").fadeIn(200);
        $(".alert-type").fadeOut(200);
    })





    function uploadEach(input_val){
        $.ajax({
            type: 'post',
            url: '/manage/buildingDesc/inserts',
            data:input_val,
            success: function (msg) {
                if (msg.code == '1') {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        window.location.href=window.location.href;
                    })
                } else {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        $('.alert-info').fadeOut(300);
                    })
                }
            }
        })
    }
    //文本上传
    $(".alert-font-add").click(function(){
        var input_val={}
        if($(this).attr("data-ids")!==''){
            input_val["id"]=$(this).attr("data-ids");
        }
        input_val["building_id"]=$(this).attr("data-id");
        input_val["role"]=$(this).attr("data-role");
        input_val["title"]=$(".alert-text-input").val();
        input_val["content"]=$(".alert-text-textarea").val();
        console.log(input_val)
        uploadEach(input_val)
        return false;
    })
    //多图上传
    $(".alert-album-add").click(function(){
        var input_val={}
        if($(this).attr("data-ids")!==''){
            input_val["id"]=$(this).attr("data-ids");
        }
        input_val["building_id"]=$(this).attr("data-id");
        input_val["role"]=$(this).attr("data-role");
        input_val["title"]=$(".album-title").val();
        input_val["img"]=allAlbum;
        console.log(input_val)
        uploadEach(input_val)
        return false;
    })

    //单图上传
    $(".alert-singlePic-add").click(function(){
        var input_val={}
        if($(this).attr("data-ids")!==''){
            input_val["id"]=$(this).attr("data-ids");
        }
        input_val["building_id"]=$(this).attr("data-id");
        input_val["role"]=$(this).attr("data-role");
        input_val["title"]=$(".alert-picture-input").val();
        input_val["img"]=$(".single-img-path").val();
        console.log(input_val)
        uploadEach(input_val)
        return false;
    })
    //图文上传
    $(".alert-article-add").click(function(){
        var input_val={}
        if($(this).attr("data-ids")!==''){
            input_val["id"]=$(this).attr("data-ids");
        }
        input_val["building_id"]=$(this).attr("data-id");
        input_val["role"]=$(this).attr("data-role");
        input_val["title"]=$(".alert-article-title").val();
        input_val["content"]=$(".alert-article-textarea").val();
        input_val["img"]=$(".article-img-path").val();
        console.log(input_val)
        uploadEach(input_val)
        return false;
    })
    //视频上传
    $(".alert-video-add").click(function(){
        var input_val={}
        input_val["building_id"]=$(this).attr("data-id");
        input_val["role"]=$(this).attr("data-role");
        input_val["title"]=$(".alert-videos-input").val();
        input_val["content"]=$(".alert-video-path").val();
        console.log(input_val)
        uploadEach(input_val)
        return false;
    })
    //调整顺序
    function newOrder(input_val){
        $.ajax({
            type: 'post',
            url: '/manage/BuildingDesc/shiftUp',
            data:input_val,
            success: function (msg) {
                console.log(msg)
                if (msg.code == '1') {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        window.location.href=window.location.href;
                    })
                } else {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        $('.alert-info').fadeOut(300);
                    })
                }
            }
        })
    }
    var currentOrder=[]
    $(".content-list").each(function(){
        var index=$(this).attr("data-id");
        currentOrder.push(index);
    })
    console.log(currentOrder)
    // 置顶
    $(".topest").click(function(){
        var id=$(this).attr("data-id");
        for(var i=0;i<currentOrder.length;i++){
            if(id==currentOrder[i]){
                var delEle=currentOrder.splice(i,1);//删除该元素
                currentOrder.unshift(id);//将其追加到开头
            }
        }
        var input_val={}
        input_val["id"]=$(".page-id").val();
        input_val["num"]=currentOrder;
        console.log(input_val)
        newOrder(input_val)
    })
    //上移
    $(".goUp").click(function(){
        // 交换数组元素
        var id=$(this).attr("data-id");
        console.log(currentOrder)
        for(var i=0;i<currentOrder.length;i++){
            if(id==currentOrder[i]){
                currentOrder.splice(i,1);//删除该元素
                currentOrder.splice(i-1, 0, id);//将其追加指定位置
            }
        }
        var input_val={}
        input_val["id"]=$(".page-id").val();
        input_val["num"]=currentOrder;
        console.log(input_val)
        newOrder(input_val)
    })
    //下移
    $(".goDown").click(function(){
        // 交换数组元素
        var index=$(this).closest(".content-list").index();
        var nextIndex=$(this).closest(".content-list").index()+1;
        //交换位置
        var nextVal=currentOrder[nextIndex]
        currentOrder[nextIndex]=currentOrder[index]
        currentOrder[index]=nextVal;
        console.log(currentOrder)
        // var id=$(this).attr("data-id");
        // console.log(currentOrder)
        // for(var i=0;i<currentOrder.length;i++){
        //     if(id==currentOrder[i]){
        //         currentOrder.splice(i,1);//删除该元素
        //         currentOrder.splice(i+1, 0, id);//将其追加指定位置
        //     }
        // }
        var input_val={}
        input_val["id"]=$(".page-id").val();
        input_val["num"]=currentOrder;
        console.log(input_val)
        newOrder(input_val)
    })
    //删除
    $(".Delete-span").click(function(){
        var id=$(this).attr("data-id");
        $.ajax({
            type: 'post',
            url: '/manage/BuildingDesc/dels',
            data:{id:id},
            success: function (msg) {
                console.log(msg)
                if (msg.code == '1') {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        window.location.href=window.location.href;
                    })
                } else {
                    $('.alert-info-font').text(msg.message);
                    $('.alert-info').fadeIn(300);
                    $('.alert-info-btn').on('click',function(){
                        $('.alert-info').fadeOut(300);
                    })
                }
            }
        })
    })
    //修改
    $(".content-info").click(function(){
        var type=$(this).closest(".content-list").attr("data-type");
        $(".alert-"+type+"").fadeIn(200);
        var title=$(this).closest(".content-list").find(".desc-title").val();
        var content=$(this).closest(".content-list").find(".desc-content").val();
        var img=$(this).closest(".content-list").find(".desc-img").val();
        var id=$(this).closest(".content-list").find(".desc-id").val();
        //图文
        $(".alert-article-title").val(title);
        $(".alert-article-textarea").val(content);
        $("#articleImg").attr("src",img);
        $(".article-img-path").val(img);
        $(".alert-article-add").attr("data-ids",id)
        //文本
        $(".alert-text-input").val(title);
        $(".alert-text-textarea").val(content);
        $(".alert-font-add").attr("data-ids",id)
        //单图
        $(".alert-picture-input").val(title);
        $(".display-singleIimg").attr("src",img);
        $(".single-img-path").val(img);
        $(".alert-singlePic-add").attr("data-ids",id)
        //视频
        $(".alert-videos-input").val(title);

        //多图
        $(".album-title").val(title);
        $(".alert-album-add").attr("data-ids",id)
        $(".img-list:not(:first-child)").remove();
        $(this).closest(".content-list").find(".desc-val-album-images").each(function(){
            var path=$(this).val();
            console.log(path)
            $(".alert-add-album-warp").append(
                '<div class="img-list">' +
                '<img class="alert-album-img" src="'+path+'" >' +
                '<img data-id="'+id+'" class="delete-alert-album-img" src="/Extend/images/delete.svg" onclick="delInput(this)">' +
                '</div>'
            )
        })
    })
})
function delInput(obj){
    $(obj).closest(".img-list").remove();
    var id=$(obj).attr("data-id");
    var img=$(obj).closest('.img-list').find(".alert-album-img").attr("src");
    console.log(id,img)
    $.ajax({
        type: 'post',
        url: '/manage/BuildingDesc/delImg',
        data: {id:id,img:img},
        success: function (msg,obj) {
            console.log(msg)
            if (msg.code == '1') {
                $('.alert-info').fadeOut(300);
            } else {
                $('.alert-info-font').text(msg.message);
                $('.alert-info').fadeIn(300);
                $('.alert-info-btn').on('click',function(){
                })
            }
        }
    })
}
